package mygang.model;

import java.io.Serializable;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.*;

/**
 *
 * @author Volodymyr Lavrynovych <volodya.lavrynovych@gmail.com>
 */
@javax.persistence.Entity
@Table(name = "USER", uniqueConstraints={@UniqueConstraint(columnNames="USER_LOGIN")})
public class User implements Serializable, OptionInterface {

    private static final long serialVersionUID = 98413576485615L;

    @Id
    @Column(name = "USER_ID")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;
    
    @Column(name = "USER_LOGIN", unique=true)
    private String login;
    
    @Column(name = "USER_PASSWORD")
    private String password;
    
    @Column(name = "USER_IS_ACTIVE")
    private boolean isActive;
    
    @Column(name = "USER_CREATION_DATE")
    @Temporal(javax.persistence.TemporalType.DATE)
    private Date creationDate;
    
    @ManyToMany(cascade = {CascadeType.REFRESH}, fetch = FetchType.EAGER)
    @JoinTable(name = "USER_ROLE",
    joinColumns = {
        @JoinColumn(name = "USER_ID")},
    inverseJoinColumns = {
        @JoinColumn(name = "ROLE_ID")})
    private Set<Role> roles;

    /**
     * Constructs an instance
     */
    public User() {
        this.roles = new HashSet<Role>();
    }
    
    /**
     * Constructs an instance for registration
     */
    public User(String login, String password) {
        this();
        this.login = login;
        this.password = password;
        this.creationDate = new Date();
        this.isActive = true;
    }
    
    public long getId() {
        return id;
    }

    public String getLogin() {
        return login;
    }

    public String getPassword() {
        return password;
    }

    public Set<Role> getRoles() {
        return roles;
    }

    public void setId(long id) {
        this.id = id;
    }

    public void setLogin(String login) {
        this.login = login;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public void setRoles(Set<Role> roles) {
        this.roles = roles;
    }

    public boolean isActive() {
        return isActive;
    }

    public void setIsActive(boolean isEnabled) {
        this.isActive = isEnabled;
    }

    public Date getCreationDate() {
        return creationDate;
    }

    public void setCreationDate(Date creationDate) {
        this.creationDate = creationDate;
    }

    public Option toOption() {
        return new Option(getId(), getLogin());
    }
}
